package com.sunshine.lnuplus.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.snackbar.Snackbar;

import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class UpdateHelper {
    public static void checkUpdate(final Activity activity, final FragmentManager fragmentManager, final boolean falseToast){
        String url = "http://www.iyuji.cn/iyuji/s/VUlpZG1WZ3A2alo2OWtxZThadHU2QT09/1575180597291559\n";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Looper.prepare();
                if(falseToast) Toast.makeText(activity,"检查更新失败",Toast.LENGTH_SHORT).show();
                Looper.loop();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                Document document = Jsoup.parse(response.body().string());
                Elements elements = document.select("p");
                showUpdate(elements.toString(),activity,fragmentManager,falseToast);
            }
        });
    }

    private static void showUpdate(String response, final Activity activity, FragmentManager fragmentManager, boolean falseToast){
        String[] update = response.replace("#","\n").split("\\*");
        String version = null;
        try {
            version = activity.getPackageManager().getPackageInfo(activity.getPackageName(),0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
//        if(update[1].equals(version)){
//            if(falseToast) {
//                Looper.prepare();
//                Toast.makeText(activity,"已经是最新版本",Toast.LENGTH_SHORT).show();
//                Looper.loop();
//            }
//        }
//        else{
//            DialogInterface.OnClickListener positive = new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    try {
//                        String str = "market://details?id=com.sunshine.lnuplus";
//                        Intent localIntent = new Intent(Intent.ACTION_VIEW);
//                        localIntent.setData(Uri.parse(str));
//                        activity.startActivity(localIntent);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            };
//            DialogInterface.OnClickListener negative = new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which) { }};
//            DialogFragmentHelper dialogFragmentHelper = DialogFragmentHelper.newInstance("辽大+有新版本啦",update[2],"去应用商店更新","取消",false,positive,negative);
//            dialogFragmentHelper.show(fragmentManager,"");
//        }
    }
}
